package work20241128;

import java.util.Arrays;

/**
 * @author zdl
 * @date 2024/11/28
 */
public class Demo4 {
    public static void main(String[] args) {
        //冒泡排序
        int[] arr = {4, 3, 2, 1};
        /**
         *  i=0 0<3 true
         *      j=0 0<3 true if(arr[0] > arr[1]) 4>3 true
         *          int temp = arr[0] =4;
         *          arr[0] = arr[0 + 1] =3; --->[3,3,2,1]
         *          arr[0 + 1] = temp =4;--->[3,4,2,1]  j++
         *     j=1 1<3 true if(arr[1]>arr[2]) 4>2 true
         *          int temp = arr[1] = 4;
         *          arr[1] = arr[1 + 1] = 2; --->[3,2,2,1]
         *          arr[1 + 1] = 4;--->[3,2,4,1]    j++
         *     j=2 2<3 true if(arr[2]>arr[3]) 4>1 true
         *          int temp = arr[2]=4;
         *          arr[2] = arr[2 + 1]=1; --->[3,2,1,1]
         *          arr[2 + 1] = 4;--->[3,2,1,4] j++
         *     j=3 3<3 false    i++
         *  i=1 1<3 true    [3,2,1,4]
         *      j=0 j<2 true if(arr[0]>arr[1]) 3>2 true
         *          int temp = arr[0]=3;
         *          arr[0] = arr[0 + 1]=2;  --->[2,2,1,4]
         *          arr[0 + 1] = 3;--->[2,3,1,4]    j++
         *      j=1 1<2 true if(arr[1]>arr[2]) 3>1 true
         *          int temp = arr[1] =3;
         *          arr[1] = arr[1 + 1]=1;--->[2,1,1,4]
         *          arr[1 + 1] = 3;--->[2,1,3,4]
         *      j=2 2<2 false i++
         *  i=2 2<3 true [2,1,3,4]
         *      j=0 0<1 true if(arr[0]>arr[1]) 2> 1 true
         *          int temp = arr[0] =2;
         *          arr[0] = arr[0 + 1]=1;--->[1,1,3,4]
         *          arr[0 + 1] = 2;--->[1,2,3,4]   j++
         *     j=1 1<1 false   i++
         *  i=3 3<3 false
         *
         */
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}
